package inner

import java.util.*
import kotlin.collections.RandomAccess

/**
 * Created by Administrator on 2019/09/29.
 */
fun main(args: Array<String>) {
    val num = mutableListOf("one", "two", "three", "four", "five")

    //返回lambda
    val num2 = num.run {
        add("six")
        println(this)
    }
    //返回当前对象
    val num3 = num.apply {
        add("seven")
        println(this)
    }
    //返回lambda
    val num4 = with(num){
        remove("seven")
        println(this)
    }
    //返回当前对象
    val num5 = num.also {
        it.remove("six")
        println(it)
    }
    //返回lambda
    val num6 = num.let {
        it.remove("one")
        println(it)
    }

    var str = "hello"
    str.takeIf { it.isNotEmpty()}?.let { println(it.first().toUpperCase())}

//    str.takeUnless { it.size > 4 }.let(::println)

    displaySubstrPosition("hello world", "lo")

    var ttv = null;
}

fun displaySubstrPosition(input : String, sub : String){
    input.indexOf(sub).takeIf { it >= 0 }?.let {
        println("the \"$sub\" in [$input] index is $it")
    }
}

val sum : (Int , Int) -> Int = {a, b -> a+b}

val sum2 = {a:Int, b: Int -> a + b}